from fastapi import APIRouter, Request
from fastapi import Depends
from config.get_db import get_db
from module_admin.entity.vo.cms_car_vo import OrderListMessageForQuery
from module_admin.entity.vo.cms_driver_vo import DriverModel, DriverPageObject, DriverForQuery, \
    OrderListForQuery
from module_admin.entity.vo.cms_order_vo import OrderPlanTime
from module_admin.service.cms_car_service import CmsCarService
from module_admin.service.cms_driver_service import CmsDriverService
from module_admin.dao.dept_dao import *
from module_admin.service.login_service import get_current_user_details
from utils.page_util import get_page_obj
from utils.response_util import *
from utils.log_util import *
from utils.time_format_util import object_format_datetime

driverController = APIRouter(dependencies=[Depends(get_current_user_details)])


@driverController.post("/record/create", response_model=DriverModel, summary="驾驶员档案-新增")
async def create_driver(request: Request, query: DriverModel, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsDriverService.create_driver(query_db, query)
        return response_200(data=dept_query_result, message="新增成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@driverController.get("/record/delete/{driver_id}", summary="驾驶员档案-删除")
async def delete_driver(request: Request, driver_id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsDriverService.delete_driver(query_db, driver_id)
        return response_200(data=dept_query_result, message="删除成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@driverController.post("/record/update", summary="驾驶员档案-编辑")
async def update_driver(request: Request, driver_update: DriverModel, db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsDriverService.update_driver(db, driver_update)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@driverController.get("/record/get/{id}", summary="驾驶员档案-详情")
async def get_driver(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsDriverService.get_driver(query_db, id)
        object_format_datetime(dept_query_result)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))

@driverController.post("/record/getNewOrderList/", summary="驾驶员档案-详情(任务信息)")
async def get_car_new(request: Request, order: OrderListMessageForQuery, query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsCarService.get_car_new_order_list(query_db, order)
        object_format_datetime(dept_query_result)
        return response_200(data=dept_query_result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@driverController.post("/record/page", summary="驾驶员档案-分页")
async def page_driver(request: Request, driver_page_query: DriverPageObject, db: Session = Depends(get_db)):
    try:
        query_object = DriverForQuery(**driver_page_query.dict())
        page_result = CmsDriverService.list(db, query_object)
        list_format_datetime(page_result)
        # 分页操作
        page_result_after = get_page_obj(page_result, driver_page_query.page_num, driver_page_query.page_size)
        # 处理
        return response_200(data=page_result_after, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@driverController.get("/record/list/{id}", summary="驾驶员档案- 空闲驾驶员下拉框")
async def record_list(request: Request, order_id: int, db: Session = Depends(get_db)):
    try:
        result = CmsDriverService.list_free(db, order_id)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@driverController.post("/record/listForUrgent", summary="驾驶员档案- 空闲驾驶员下拉框(紧急)")
async def record_list_for_urgent(request: Request, order_plan_time: OrderPlanTime, db: Session = Depends(get_db)):
    try:
        result = CmsDriverService.list_free_for_urgent(db, order_plan_time)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@driverController.get("/record/listAll", summary="驾驶员档案- 全部驾驶员下拉框")
async def record_list_all(request: Request, order_id_num: int = 0, db: Session = Depends(get_db)):
    try:
        result = CmsDriverService.list_all(db, order_id_num)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))

@driverController.post("/record/listAllForUrgent", summary="驾驶员档案- 全部驾驶员下拉框(紧急)")
async def record_list_all_for_urgent(request: Request, order_plan_time: OrderPlanTime, db: Session = Depends(get_db)):
    try:
        result = CmsDriverService.list_all_for_urgent(db, order_plan_time)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@driverController.post("/record/orderList", summary="驾驶员档案-任务列表")
async def page_driver(request: Request, driver_order_query: OrderListForQuery, db: Session = Depends(get_db)):
    try:
        result = CmsDriverService.order_list(db, driver_order_query)
        list_format_datetime(result)
        # 处理
        return response_200(data=result, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
